using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LibraryAutomation.PeriodicalPublications
{
    public partial class frmPBDelete : Form
    {
        int rowCount;
        DateTime release_date;

        public frmPBDelete()
        {
            InitializeComponent();
            rowCount = 10;
            release_date = new DateTime();
            dataGridView1.ColumnCount = 3;
            dataGridView1.RowCount = rowCount;
            dataGridView1.Columns[0].HeaderCell.Value = "Name";
            dataGridView1.Columns[2].HeaderCell.Value = "Release date";
            dataGridView1.Columns[1].HeaderCell.Value = "Issue";
        }

        private void frmPBDelete_Load(object sender, EventArgs e)
        {
            fillWithPP();
        }

        private void fillWithPP()
        {
            //lbxTopics.Items.Clear();

            string stSQL = "SELECT pp_name, release_date, issue FROM periodical_publications ORDER BY pp_name ASC";
            DataSet ds = new DataSet();

            if (datalayer.GetDataFromDB(datalayer.default_NetLibDB_Projeler_ConnectionString, stSQL, ref ds))
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count - rowCount > 0)
                        dataGridView1.Rows.Add(ds.Tables[0].Rows.Count - rowCount);

                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dataGridView1.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i].ItemArray[0].ToString();
                        release_date = Convert.ToDateTime(ds.Tables[0].Rows[i].ItemArray[1]);
                        dataGridView1.Rows[i].Cells[2].Value = release_date.ToShortDateString();
                        dataGridView1.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i].ItemArray[2].ToString();
                    }
                }
            }
        }


        private bool DeletePP(string ppName, int ppIssue)
        {
            string stSQL = "DELETE FROM periodical_publications WHERE pp_name = '" + ppName + "' AND issue = '"+ppIssue+"'";
            int rowsAffected = 0;

            if (datalayer.Execute(datalayer.default_NetLibDB_Projeler_ConnectionString, stSQL, ref rowsAffected))
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0) {
                string name = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                int issue = Convert.ToInt16(dataGridView1.SelectedRows[0].Cells[1].Value);

                if (DeletePP(name, issue))
                {
                    dataGridView1.Rows.Clear();// Columns.Clear();
                    rowCount = 10;
                    dataGridView1.ColumnCount = 3;
                    dataGridView1.RowCount = rowCount;
                    dataGridView1.Columns[0].HeaderCell.Value = "Name";
                    dataGridView1.Columns[2].HeaderCell.Value = "Release date";
                    dataGridView1.Columns[1].HeaderCell.Value = "Issue";
                    fillWithPP();
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}